package com.ruoyi.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.vo.AjaxResult;
import com.ruoyi.controller.BaseController;
import com.ruoyi.domain.SysConfig;
import com.ruoyi.service.SysConfigService;



@RestController
@RequestMapping("system/config")
public class SysConfigController extends BaseController{
	
	
	@Autowired
	private SysConfigService sysConfigService;
	
	
	/**
	 * 获取参数列表
	 * @param config
	 * @return
	 */
	@GetMapping("list")
	public TableDataInfo list(SysConfig config) {
		logger.info("获取参数列表，config:{}",config);
		startPage();
		List<SysConfig> list = sysConfigService.selectConfigList(config);
		return getDataTable(list);
	}
	
	/**
	 * 根据参数键名查询参数值
	 * @param configKey
	 * @return
	 */
	@GetMapping(value = "configKey/{configKey}")
	public AjaxResult getConfigKey(@PathVariable String configKey) {
		logger.info("根据参数键名查询参数值,configKey:{}",configKey);
		return AjaxResult.success(sysConfigService.selectConfigByKey(configKey));
	}
	
	/**
	 * 根据参数编号获取详细信息
	 * @param configId
	 * @return
	 */
	@GetMapping(value = "/{configId}")
	public AjaxResult getInfo(@PathVariable Long configId) {
		logger.info("根据参数编号获取详细信息,configId:{}",configId);
		return AjaxResult.success(sysConfigService.selectConfigById(configId));
	}
	
	/**
	 * 新增参数
	 * @param config
	 * @return
	 */
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysConfig config) {
		logger.info("新增参数,config:{}",config);
		if (!sysConfigService.checkConfigKeyUnique(config)) {
			return error ("新增参数"+config.getConfigKey() + "失败，参数键名已存在");
		}
		config.setCreateBy(getUsername());
		return toAjax(sysConfigService.insertConfig(config));
	}
	
	/**
	 * 修改参数
	 * @param config
	 * @return
	 */
	@PutMapping
	
	public AjaxResult edit(@Validated @RequestBody SysConfig config) {
		// {"createBy":"admin","createTime":"2023-11-17T06:53:27.000+08:00","updateBy":"","updateTime":null,"remark":"测试代码","params":{},"configId":7,"configName":"测试","configValue":"TT","configKey":"test","configType":"Y"}
		logger.info("修改参数,config:{}",config);
		if (!sysConfigService.checkConfigKeyUnique(config)) {
			return error ("修改参数"+config.getConfigName() + "失败，参数键名已存在");
		}
		config.setUpdateBy(getUsername());
		return toAjax(sysConfigService.updateConfig(config));
	}
	
	/**
	 * 删除参数
	 * @param configIds
	 * @return
	 */
	@DeleteMapping("/{configIds}")
	public AjaxResult remove(@PathVariable Long[] configIds) {
		logger.info("删除参数,configIds:{}", JSON.toJSONString(configIds));
		sysConfigService.deleteConfigByIds(configIds);
		return AjaxResult.success();
	}
}
